﻿# -- Dice_m.py
"""
    Copyright 2013 J. Kent Harbaugh

    This file is part of Raddel.

    Raddel is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raddel is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raddel.  If not, see <http://www.gnu.org/licenses/>.
"""



class Dice_t:
    def Roll(Self):
        roll=0
        idx=Num
        while idx>=1:
            roll+=random.random()*Self._Die+0.5
            idx-=1
        if idx>0:
            roll+=(random.random()*Self._Die+0.5)*idx
        return roll
    def __call__(Self):
        return Self.Roll()
    def __init__(Self,Arg1=None,Arg2=None):
        # Dice_t(Die)
        # Dice_t(Num,Die)
        # Dice_t('NdD')
        Self._Num=1
        Self._Die=6
        if isinstance(Arg1,int) or isinstance(Arg1,float):
            if isinstance(Arg2,int) or isinstance(Arg2,float):
                Self._Num=Arg1
                Self._Die=Arg2
            else:
                Self._Die=Arg1
        elif isinstance(Arg1,str):
            try:
                Arg1=Arg1.lower()
                idx=Arg1.find('d')
                if idx==-1:
                    raise ValueError()
                elif idx==0:
                    Self._Die=float(Arg1[idx+1:])
                else:
                    Self._Num=float(Arg1[0:idx])
                    if idx+1<len(Arg1):
                        Self._Die=float(Arg1[idx+1:])
            except:
                raise ValueError('Dice_t: cannot parse \''+Arg1+'\'')
        if Self._Num<0:
            raise ValueError('Dice_t: invalid number of dice')
        if Self._Die<1:
            raise ValueError('Dice_t: invalid size of dice')
    def __repr__(Self):
        return 'Dice_t(\''+Self.__str__()+'\')'
    def __str__(Self):
        s=''
        if Self._Num!=1:
            s+=str(Self._Num)
        s+='d'+str(Self._Die)
        return s



import random



# -- Dice_m.py
